package com.winphone.zhgd.web;

import com.baomidou.mybatisplus.plugins.Page;
import com.winphone.zhgd.config.WebConfig;
import com.winphone.zhgd.entity.TbSysUser;
import com.winphone.zhgd.entity.User;
import com.winphone.zhgd.service.ITbSysUserService;
import com.winphone.zhgd.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Package: com.winphone.zhgd.web
 * Description : TODO 系统用户模块
 * User: zhou
 * Date: 2017/8/3116:50
 * version 1.0.0
 * see:
 */
@Controller
@RequestMapping("/sys")
public class SysUserController {

    @Autowired
    private ITbSysUserService ss;

    /**
     * 登录
     */
    @RequestMapping(value = "/login")
    public String login() {
        System.out.println("12");
        return "login1";
    }

    /**
     * 登录请求
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> loginPost(String account, String password, HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        //此处采用手动判别
        int returnCode = 1;
        String returnMessage ;
        if (StringUtils.isEmpty(account)){
            map.put("success", 3);
            map.put("message", "参数错误");
            return map;
        }
        if (StringUtils.isEmpty(password)){
            map.put("success", 3);
            map.put("message", "参数错误");
            return map;
        }
        TbSysUser sysUser = ss.getSysUser(account,password);

        if (null == sysUser){
            map.put("success", 2);
            map.put("message", "密码错误");
            return map;
        }
        // 设置session
        session.setAttribute(WebConfig.SESSION_KEY, account);

        map.put("success", 1);
        map.put("message", "登录成功");
        return map;
    }

    /**
     * 登出
     * @param session
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        // 移除session
        session.removeAttribute(WebConfig.SESSION_KEY);
        return "redirect:/login";
    }

}
